//
//  RCtabBar.m
//  RCsinaMicroBlog
//
//  Created by Mr.Ray on 15/1/29.
//  Copyright (c) 2015年 Ray.peng. All rights reserved.
//

#import "RCtabBar.h"
#import "RCtabBarButton.h"

@interface RCtabBar ()

@property (nonatomic, strong)RCtabBarButton *selectedBtn;
/**
 *  存储所有的选项卡按钮
 */
@property (nonatomic, strong)NSMutableArray *btns;
/**
 *  加号按钮
 */
@property (nonatomic, weak) UIButton *plusBtn;

@end

@implementation RCtabBar

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
    [self setBackgroundColor:[UIColor whiteColor]];
    [self addPlushBtn];
}

/**
 *  创建加号按钮
 */
- (void)addPlushBtn
{
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    // 4.添加
    [self addSubview:plusBtn];
    
    self.plusBtn = plusBtn;
    
}

- (void)btnClick:(RCtabBarButton *)btn{
    self.selectedBtn.selected = !self.selectedBtn.selected;
    
    btn.selected = !btn.selected;
    
    self.selectedBtn = btn;
    
    //点击后让按钮放大缩小
    [UIView animateWithDuration:0.2 animations:^{
        btn.transform = CGAffineTransformMakeScale(0.6, 0.6);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 animations:^{
                btn.transform = CGAffineTransformIdentity;
            }];
        }];
    }];
    
    //添加代理
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedBtnFrom:to:)]) {
        [self.delegate tabBar:self selectedBtnFrom:self.selectedBtn.tag to:btn.tag];
    }
}

- (void)addItem:(UITabBarItem *)item{
    //创建选项卡
    RCtabBarButton *btn = [[RCtabBarButton alloc]init];
    btn.item = item;
    [self addSubview:btn];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.tag = self.btns.count;
    [self.btns addObject:btn];
    //默认选中第一个界面
    if (self.btns.count == 1) {
        [self btnClick:btn];
    }
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    NSUInteger index = 0;
    DDLogDebug(@"%@",self.subviews);
    
    for (UIView *child in self.subviews) {
        if ([child isKindOfClass:[RCtabBarButton class]]) {
            
            CGFloat W = self.frame.size.width / 5;
            CGFloat H = self.frame.size.height;
            CGFloat X = index * W;
            CGFloat Y = 0;
            
            if (index == 2) {
                index++;
                X = index * W;
            }
            
            index++;
            child.frame = CGRectMake(X, Y, W, H);
            
        }
    }
    // 设置加号按钮的frame
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
}

- (NSMutableArray *)btns{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}
@end
